/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.net.dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package netty.mqtt;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.codec.ByteBufferUtil;
import net.dreamlu.iot.mqtt.core.client.MqttClient;
import netty.lora.SundaeRSTask;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class MqttDevice {
    public static void main(String[] args) {
        // 初始化 mqtt 客户端
        MqttClient client = MqttClient.create()
                .ip("127.0.0.1")
                .port(1883)
                .connect();

        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(4);
        schedule.scheduleAtFixedRate(
                new Runnable() {
                    @Override
                    public void run() {
                        MqttCommonMsg msg = new MqttCommonMsg();
                        msg.setSpid(UUID.randomUUID().toString());
                        msg.setTid("DB6661");
                        msg.setType("ACK");
                        msg.setTime(Instant.now().getEpochSecond());

                        BeverageMixerData data = new BeverageMixerData();
                        data.setWorkMode("手动模式");
                        data.setWorkNumber(100);
                        data.setWorkState("屏保状态");
                        data.setWorkStatus(1);
                        data.setIsolationCoverState(true);

                        msg.setData(new Gson().toJson(data));
                        client.publish("device/drinkblender/pub", ByteBuffer.wrap(new Gson().toJson(msg).getBytes()));
                    }
                }, 5, 10, TimeUnit.SECONDS);
    }

}
